package org.linlinjava.litemall.core.util;

import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tiia.v20190529.TiiaClient;
import com.tencentcloudapi.tiia.v20190529.models.ImageModerationRequest;
import com.tencentcloudapi.tiia.v20190529.models.ImageModerationResponse;

public class PictureUtil {

    public static JSONObject judgeImage(String url) throws TencentCloudSDKException {
        Credential cred = new Credential("AKIDXqy5jACmW3LNGuvQVlnbVIBqFQN4Hs1u", "pRpzvbKq66rOgiUM2Z6TeeSRWO0NUO5Y");

        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("tiia.tencentcloudapi.com");

        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        TiiaClient client = new TiiaClient(cred, "ap-beijing", clientProfile);

        String params = "{\"Scenes\":[\"PORN\"],\"ImageUrl\":\"" + url + "\"}";
        ImageModerationRequest req = ImageModerationRequest.fromJsonString(params, ImageModerationRequest.class);

        ImageModerationResponse resp = client.ImageModeration(req);
        String result = ImageModerationRequest.toJsonString(resp);
        return JSONObject.parseObject(result);
    }

    public static JSONObject judgePornImage(String url) throws TencentCloudSDKException {
        JSONObject result = judgeImage(url);
        return result.getJSONObject("PornResult");
    }

    public static boolean checkImage(String url) throws TencentCloudSDKException {
        JSONObject result = judgeImage(url);
        return ("PASS".equalsIgnoreCase(result.getString("Suggestion")));
    }

    public static boolean checkPornImage(String url) throws TencentCloudSDKException {
        JSONObject result = judgePornImage(url);
        return ("PASS".equalsIgnoreCase(result.getString("Suggestion")));
    }

    public static void main(String[] args) {
        try {
            long start = System.currentTimeMillis();
            System.out.println(start);
            System.out.println(checkImage("http://sms.slzhjc.com/litemall/wx/storage/fetch/rfie405xn4kpbid74rfq.jpg"));
            System.out.println(System.currentTimeMillis());
            System.out.println(System.currentTimeMillis() - start);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }
}
